package com.example.android.debuggermenu;

import java.util.HashMap;
import android.content.Context;
import android.media.AudioManager;
import android.media.SoundPool;
import android.os.Vibrator;
import android.util.Log;

/**
 * Source: http://www.droidnova.com/creating-sound-effects-in-android-part-1,570.html
 * Author:Stephen Flockton OCT 20 2009
 */

public class SoundManager {
    public boolean released=false;
    protected Context context;
    private SoundPool soundPool;
    private float volume;
    private HashMap<String, Integer> soundPoolMap;
    protected SoundManager(int size, Context context) {
        this.context=context;
        
        soundPool=new SoundPool(size+20, AudioManager.STREAM_MUSIC, 40);
        soundPoolMap = new HashMap<String, Integer>();
        AudioManager mgr=(AudioManager)context.getSystemService(Context.AUDIO_SERVICE);
        volume=mgr.getStreamVolume(AudioManager.STREAM_MUSIC);
        //volume=volume-10.0f;
        System.out.println("volume is: "+volume);
                 
    }
    /*
    public boolean isLoaded() {
       soundPool.
    }
    */

    public void addSound(String resid) {
        int soundId=soundPool.load(resid, 1);
        soundPoolMap.put(resid, soundId);
        soundPool.setLoop(soundId, 1);
    }

    public void addLoopSound(String resid) {
        int soundId=soundPool.load(resid, 1);
        soundPoolMap.put(resid, soundId);
        soundPool.setLoop(soundId, -1);
    }

    public void play(String resid) {
        Log.d("AudioManager", "Playing: "+resid);
        int soundId=soundPoolMap.get(resid);
        soundPool.setLoop(soundId, 1);
        soundPool.play(soundId, volume, volume, 1, 0, 1f);
    }

    public void playLoop(String resid) {
    	Log.d("AudioManager", "Playing: "+resid);
        int soundId=soundPoolMap.get(resid);
        soundPool.setLoop(soundId, -1);
        soundPool.play(soundId, volume, volume, 1, 0, 1f);;
    }

    public void stop(String resid) {
        //soundPool.stop(resid);
        // apperently a bug, workaround
        int soundId=soundPoolMap.get(resid);
        soundPool.setLoop(soundId, 0);
        soundPool.setVolume(soundId, 0f, 0f);
    }

    public void release() {
        released=true;
        soundPool.release();
    } 
}
